package com.lagou.dao;

import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;

import java.beans.IntrospectionException;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl  implements IUserDao{
    @Override
    public List<User> findAll() throws SQLException, IntrospectionException, NoSuchFieldException, ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException, PropertyVetoException, DocumentException {

        //获取类加载器，通过类加载器获取资源的字节输入流
        InputStream resourceAsSteam= Resources.getResourceAsSteam("sqlMapConfig.xml");
        //创建sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();


        List<User> users=sqlSession.selectList("user.selectList");
        for (User object : users) {
            System.out.println(object);
        }

        return users;
    }

    @Override
    public User findByCondition(User user) throws SQLException, IntrospectionException, NoSuchFieldException, ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException, PropertyVetoException, DocumentException {

        //获取类加载器，通过类加载器获取资源的字节输入流
        InputStream resourceAsSteam= Resources.getResourceAsSteam("sqlMapConfig.xml");
        //创建sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //调用
        User user2 = sqlSession.selectOne("user.findByCondition", user);
        System.out.println(user2);



        return user2;
    }
}
